package com.kfm.am;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class JDBCDemo {

    public static void main(String[] args) throws Exception {
        // 加载配置文件 --> Properties
        Properties prop = new Properties();
        prop.load(new FileInputStream("io/datasource.properties"));
        // 通过配置创建 DataSource
        DataSource dataSource = DruidDataSourceFactory.createDataSource(prop);
        // 获取连接对象
        Connection connection = dataSource.getConnection();
        // 获取执行 sql的对象
        PreparedStatement ps = connection.prepareStatement("select * from student");
        // 执行sql
        ResultSet resultSet = ps.executeQuery();
        // 获取列数
        int columnCount = resultSet.getMetaData().getColumnCount();
        while (resultSet.next()) {
            for (int i = 1; i <= columnCount; i ++) {
                System.out.print(resultSet.getObject(i) + "\t");
            }
            System.out.println();
        }
    }
}
